﻿// (c) Copyright 2013 Mike Riedel.
// This source is subject to the Microsoft Public License (Ms-PL).
// All other rights reserved.
using System;

namespace FreshMeat.Storage
{
   /// <summary>
   /// Mapper that doesn't actually do anything.
   /// </summary>
   /// <typeparam name="TId"></typeparam>
   /// <typeparam name="TItem"></typeparam>
   public class IdentityMapper<TId, TItem> : IMapper<TId, TItem, TItem>
   {
      readonly Func<TItem, TId> idGetter;

      /// <summary>
      /// Creates instance.
      /// </summary>
      /// <param name="idGetter">Function to get the ID from an item.</param>
      public IdentityMapper(Func<TItem, TId> idGetter)
      {
         this.idGetter = idGetter;
      }

      /// <summary>
      /// Maps item onto its data representation.
      /// </summary>
      /// <param name="item">Item to map.</param>
      /// <returns>Data representation.</returns>
      public TItem ToData(TItem item)
      {
         return item;
      }

      /// <summary>
      /// Maps data representation back onto item.
      /// </summary>
      /// <param name="data"></param>
      /// <returns></returns>
      public TItem ToItem(TItem data)
      {
         return data;
      }

      /// <summary>
      /// Gets an item's ID.
      /// </summary>
      /// <param name="item"></param>
      /// <returns></returns>
      public TId ToId(TItem item)
      {
         return idGetter(item);
      }
   }
}